package com.foxit.setting;

import java.io.*;
import java.net.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.UnknownHostException;
import java.util.Date;
import java.sql.Timestamp;
import java.util.TimeZone;
import java.util.SimpleTimeZone;
import java.text.SimpleDateFormat;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class SetNew extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                    throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
       request.setCharacterEncoding("UTF-8");
       PrintWriter out = response.getWriter();
       String year=request.getParameter("year"); 
       String month=request.getParameter("month"); 
       String day=request.getParameter("day"); 
       String hour=request.getParameter("hour"); 
       String min=request.getParameter("min"); 
       String copy=request.getParameter("copy"); 
       String modify=request.getParameter("modify"); 
       
       try {
          MongoClient mongo = new MongoClient("192.201.108.97", 27017);
          DB db = mongo.getDB("settingdb");
          //SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          //formatter.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
          //SimpleDateFormat sfd= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
          //sfd.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
          //DBCollection table;
          DBCollection table = db.getCollection("setlog");
          Date now = new Date();
          String dd= year+month+day+hour+min;
          if(copy != null && copy.length()!= 0) {
             BasicDBObject query = new BasicDBObject("start", copy);
             
             // table setlog 
             BasicDBObject doc = new BasicDBObject("start",dd)
                             .append("create_time",now)
                             .append("modify_time",now)
                             .append("online",0);
             table.insert(doc); 
             // table adrule
             DBCollection table1 = db.getCollection("adrule");
             //BasicDBObject query1 = new BasicDBObject("start", copy);
             DBCursor cursor1 = table1.find(query);
             while (cursor1.hasNext()) {
                Adrule s = (Adrule) JSON.parseObject(cursor1.next().toString(),Adrule.class);
                BasicDBObject doc1 = new BasicDBObject("start",dd)
                   .append("adrule",s.getAdrule())
                   .append("advertiser",s.getAdvertiser())
                   .append("related",s.getRelated());
                table1.insert(doc1);
             }
             cursor1.close();
             // table adid
             DBCollection table2 = db.getCollection("adid");
             //BasicDBObject query2 = new BasicDBObject("start", copy);
             DBCursor cursor2 = table2.find(query);
             while (cursor2.hasNext()) {
                Adid s = (Adid) JSON.parseObject(cursor2.next().toString(),Adid.class);
                BasicDBObject doc2 = new BasicDBObject("start",dd)
                   .append("adrule",s.getAdrule())
                   .append("lang",s.getLang())
                   .append("adid",s.getAdid())
                   .append("adurl",s.getAdurl())
                   .append("adimg",s.getAdimg());
                table2.insert(doc2);
             }
             cursor2.close();
             // table top 
             DBCollection table3 = db.getCollection("top");
             //BasicDBObject query3 = new BasicDBObject("start", copy);
             DBCursor cursor3 = table3.find(query);
             while (cursor3.hasNext()) {
                Top s = (Top) JSON.parseObject(cursor3.next().toString(),Top.class);
                BasicDBObject doc3 = new BasicDBObject("start",dd)
                   .append("server",s.getServer())
                   .append("groupname",s.getGroupname())
                   .append("statsday",s.getStatsday())
                   .append("count",s.getCount());
                table3.insert(doc3);
             }
             cursor3.close();
             // table  action
             DBCollection table4 = db.getCollection("action");
             DBCursor cursor4 = table4.find(query);
             while (cursor4.hasNext()) {
                Action s = (Action) JSON.parseObject(cursor4.next().toString(),Action.class);
                BasicDBObject doc4 = new BasicDBObject("start",dd)
                   .append("name",s.getName())
                   .append("motion",s.getMotion())
                   .append("high",s.getHigh())
                   .append("medium",s.getMedium())
                   .append("low",s.getLow());
                table4.insert(doc4);
             }
             cursor4.close();
             // table adana
             DBCollection table5 = db.getCollection("adana");
             //query5 = new BasicDBObject("start", copy);
             DBCursor cursor5 = table5.find(query);
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'Z'");
             sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
             while (cursor5.hasNext()) {
                Adana s = (Adana) JSON.parseObject(cursor5.next().toString(),Adana.class);
                BasicDBObject doc5 = new BasicDBObject("start",dd)
                   .append("id",s.getId())
                   .append("grp",s.getGrp())
                   .append("log",s.getLog())
                   .append("expr",s.getExpr())
                   .append("count",s.getCount())
                   .append("modify_time",sdf.parse(s.getModify_time()));
                table5.insert(doc5);
             }
             cursor5.close();
             // table adanarule
             DBCollection table6 = db.getCollection("adanarule");
             //query6 = new BasicDBObject("start", copy);
             DBCursor cursor6 = table6.find(query);
             while (cursor6.hasNext()) {
                Adanarule s = (Adanarule) JSON.parseObject(cursor6.next().toString(),Adanarule.class);
                BasicDBObject doc6 = new BasicDBObject("start",dd)
                   .append("dimen",s.getDimen())
                   .append("rule",s.getRule())
                   .append("grp",s.getGrp())
                   .append("statsday",s.getStatsday())
                   .append("low",s.getLow())
                   .append("medium",s.getMedium())
                   .append("high",s.getHigh());
                table6.insert(doc6);
             }
             cursor6.close();
             //table default
             DBCollection table7 = db.getCollection("default");
             //query7 = new BasicDBObject("start", cpdate);
             DBCursor cursor7 = table7.find(query);
             while (cursor7.hasNext()) {
                Default s = (Default) JSON.parseObject(cursor7.next().toString(),Default.class);
                BasicDBObject doc7 = new BasicDBObject("start",dd)
                   .append("name",s.getName())
                   .append("adlist",s.getAdlist());
                table7.insert(doc7);
             }
             cursor7.close();
             //table groupad
             DBCollection table8 = db.getCollection("groupad");
             DBCursor cursor8 = table8.find(query);
             while (cursor8.hasNext()) {
                Groupad s = (Groupad) JSON.parseObject(cursor8.next().toString(),Groupad.class);
                BasicDBObject doc8 = new BasicDBObject("start",dd)
                   .append("name",s.getName())
                   .append("high",s.getHigh())
                   .append("medium",s.getMedium())
                   .append("low",s.getLow());
                table8.insert(doc8);
             }
             cursor8.close();
             //table grouptype
             DBCollection table9 = db.getCollection("grouptype");
             DBCursor cursor9 = table9.find(query);
             while (cursor9.hasNext()) {
                Grouptype s = (Grouptype) JSON.parseObject(cursor9.next().toString(),Grouptype.class);
                BasicDBObject doc9 = new BasicDBObject("start",dd)
                   .append("server",s.getServer())
                   .append("show_mode",s.getShow_mode())
                   .append("groupno",s.getGroupno())
                   .append("type",s.getType())
                   .append("adrule",s.getAdrule());
                table9.insert(doc9);
             }
             cursor9.close();
             //table impmsgad
             DBCollection table10 = db.getCollection("impmsgad");
             DBCursor cursor10 = table10.find(query);
             while (cursor10.hasNext()) {
                Impmsgad s = (Impmsgad) JSON.parseObject(cursor10.next().toString(),Impmsgad.class);
                BasicDBObject doc10 = new BasicDBObject("start",dd)
                   .append("name",s.getName())
                   .append("adlist",s.getAdlist());
                table10.insert(doc10);
             }
             cursor10.close();
             //table member
             DBCollection table11 = db.getCollection("member");
             //query11 = new BasicDBObject("start", copy);
             DBCursor cursor11 = table11.find(query);
             while (cursor11.hasNext()) {
                Member s = (Member) JSON.parseObject(cursor11.next().toString(),Member.class);
                BasicDBObject doc11 = new BasicDBObject("start",dd)
                   .append("member",s.getMember())
                   .append("type",s.getType())
                   .append("memo",s.getMemo());
                table11.insert(doc11);
             }
             cursor11.close();
             //table policy
             DBCollection table12 = db.getCollection("policy");
             DBCursor cursor12 = table12.find(query);
             while (cursor12.hasNext()) {
                Policy s = (Policy) JSON.parseObject(cursor12.next().toString(),Policy.class);
                BasicDBObject doc12 = new BasicDBObject("start",dd)
                   .append("type",s.getType())
                   .append("show_time_every_hour",s.getShow_time_every_hour())
                   .append("show_mode",s.getShow_mode());
                table12.insert(doc12);
             }
             cursor12.close();
             //table round
             DBCollection table13 = db.getCollection("round");
             DBCursor cursor13 = table13.find(query);
             while (cursor13.hasNext()) {
                Round s = (Round) JSON.parseObject(cursor13.next().toString(),Round.class);
                BasicDBObject doc13 = new BasicDBObject("start",dd)
                   .append("name",s.getName())
                   .append("adlist",s.getAdlist());
                table13.insert(doc13);
             }
             cursor13.close();
             //table server
             DBCollection table14 = db.getCollection("server");
             //query14 = new BasicDBObject("start", cpdate);
             DBCursor cursor14 = table14.find(query);
             while (cursor14.hasNext()) {
                Server s = (Server) JSON.parseObject(cursor14.next().toString(),Server.class);
                BasicDBObject doc14 = new BasicDBObject("start",dd)
                   .append("server",s.getServer())
                   .append("show_mode",s.getShow_mode())
                   .append("groupno",s.getGroupno());
                table14.insert(doc14);
             }
             cursor14.close();

             response.sendRedirect("./Slist?modify="+dd);
             return;
          }
          if(modify != null && modify.length()!=0) {
             BasicDBObject n=new BasicDBObject("start",dd)
                             .append("create_time",now)
                             .append("modify_time",now)
                             .append("online",0);
             table.insert(n); 
             if(modify.equals("new")) {
                response.sendRedirect("./Slist?modify="+dd);
                return;
             }
          }
  
          out.println("<HTML><head><style>table,th,td { border:1px solid black;border-collapse:collapse; } </style></head><body>");
          out.println("<FORM action='./SetNew'><table border='1' style='width:300px'><tr><th colspan=3>FOXIT Setting Tool</th></tr>");
          out.println("<input type='hidden' name=year value='"+year+"'>");
          out.println("<input type='hidden' name=month value='"+month+"'>");
          out.println("<input type='hidden' name=day value='"+day+"'>");
          out.println("<input type='hidden' name=hour value='"+hour+"'>");
          out.println("<input type='hidden' name=min value='"+min+"'>");
        
          //DBCollection table = db.getCollection("setlog");
          DBCursor cursor = table.find();
          while (cursor.hasNext()) {
             Setlog s = (Setlog) JSON.parseObject(cursor.next().toString(),Setlog.class);
             if(s.getOnline()!= 1) {
                out.println("<tr><td>"+s.getStart()+"</td><td>"+s.getStart()+"</td><td><button name='copy' value='"+s.getStart()+"'>複製</button></td></tr>");
             } else {
                out.println("<tr><td>"+s.getStart()+"</td><td>"+s.getStart()+"</td><td><button name='copy' value='"+s.getStart()+"'>複製</button></td><font color=red>線上</font></tr>");
             }
          }
          out.println("<tr><td colspan=3><button name='modify' value='new'>全新</button></td></tr");
          out.println("</table></FORM><br></body></HTML>");
       //} catch (UnknownHostException e) {
       //} catch (MongoException e) {
       } catch (Exception ex) {
          out.println(ex.getMessage());
       }

    }
} 
